//
//  AppDelegate.swift
//  SouWeiLaiSwift
//
//  Created by yxl on 2022/4/11.
//

import UIKit
import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        /// 注册用户信息数据库
        RealmTools.configRealm(userID: kUD_UserInfo)
        /// 初始化window
        initWindow()
        /// 键盘初始化及设置
        conFigIQKeyboardManager()
        return true
    }
    
    /// 初始化window
    func initWindow() {
        self.window = UIWindow.init()
        self.window?.backgroundColor = UIColor.white
        self.window?.frame = UIScreen.main.bounds
        self.window?.makeKeyAndVisible()
        let models = RealmTools.objects(LoginModel.self);
        let userModel = models?.first
        if userModel != nil {
            self.window?.rootViewController = BaseNavigationController.init(rootViewController: HomeVC())
            UserManager.sharedManager.userModel = userModel as! LoginModel
            switchRootToTabbarVC()
        }else{
            switchRootToLoginVC()
        }
    }
    
    /// 键盘处理
    func conFigIQKeyboardManager() {
        IQKeyboardManager.shared.enable = true
        IQKeyboardManager.shared.enableAutoToolbar = true //工具条
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true //点击背景收回键盘
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "完成"
        IQKeyboardManager.shared.toolbarTintColor = .lightGray
        IQKeyboardManager.shared.keyboardDistanceFromTextField = 5.0
    }
    
    /// 切换更目录至登录
    func switchRootToLoginVC(){
        self.window?.rootViewController = BaseNavigationController.init(rootViewController: LoginTypeVC())
    }
    /// 切换根目录至tabbar
    func switchRootToTabbarVC(){
        self.window?.rootViewController = BaseTabBarController()
    }
    
    // MARK: - 实时网络监测
    func monitorNetwork() {
        ReachabilityTool.monitorNetworkStatus1 { status in
            var message = ""
            switch status {
            case .unknown:
                message = "未知网络"
            case .notReachable:
                message = "无网络连接"
            case .wwan:
                message = "蜂窝移动网络"
            case .ethernetOrWiFi:
                message = "WiFi"
            }
            Log("当前网络状态===：\(message)")
        }
    }
}

